﻿/**
 * @file		ct_dd_pmc_gpio.c
 * @brief		PMC GPIO driver Component Test
 * @note		None
 * @attention	None
 * 
 * <B><I>Copyright 2016 Socionext Inc.</I></B>
 */

#include <stdlib.h>
#include <string.h>
#include "driver_common.h"
#include "ct_dd_pmc_gpio.h"
#include "dd_pmc_gpio.h"
#include "pmc.h"

/*----------------------------------------------------------------------*/
/* Definition															*/
/*----------------------------------------------------------------------*/
// Nothing Special

/*----------------------------------------------------------------------*/
/* Enumeration															*/
/*----------------------------------------------------------------------*/
// Nothing Special

/*----------------------------------------------------------------------*/
/* Structure															*/
/*----------------------------------------------------------------------*/
// Nothing Special

/*----------------------------------------------------------------------*/
/* Global Data															*/
/*----------------------------------------------------------------------*/
// Nothing Special

/*----------------------------------------------------------------------*/
/* Local Method Definition												*/
/*----------------------------------------------------------------------*/
// Nothing Special

/*----------------------------------------------------------------------*/
/* Local Function														*/
/*----------------------------------------------------------------------*/
static E_DD_PMC_GPIO_PORT ct_dd_pmc_gpio_convert_port_num(int port_num)
{
	E_DD_PMC_GPIO_PORT	port = E_DD_PMC_GPIO_PORT_MAX;
	
	switch (port_num) {
		case 0:
			port = E_DD_PMC_GPIO_P00;
			break;
		case 1:
			port = E_DD_PMC_GPIO_P01;
			break;
		case 2:
			port = E_DD_PMC_GPIO_P02;
			break;
		case 3:
			port = E_DD_PMC_GPIO_P03;
			break;
		case 4:
			port = E_DD_PMC_GPIO_P04;
			break;
		case 5:
			port = E_DD_PMC_GPIO_P05;
			break;
		case 6:
			port = E_DD_PMC_GPIO_P06;
			break;
		case 7:
			port = E_DD_PMC_GPIO_P07;
			break;
		case 10:
			port = E_DD_PMC_GPIO_P10;
			break;
		case 11:
			port = E_DD_PMC_GPIO_P11;
			break;
		case 12:
			port = E_DD_PMC_GPIO_P12;
			break;
		case 13:
			port = E_DD_PMC_GPIO_P13;
			break;
		case 14:
			port = E_DD_PMC_GPIO_P14;
			break;
		case 15:
			port = E_DD_PMC_GPIO_P15;
			break;
		case 16:
			port = E_DD_PMC_GPIO_P16;
			break;
		case 17:
			port = E_DD_PMC_GPIO_P17;
			break;
		case 20:
			port = E_DD_PMC_GPIO_P20;
			break;
		case 21:
			port = E_DD_PMC_GPIO_P21;
			break;
		case 22:
			port = E_DD_PMC_GPIO_P22;
			break;
		case 23:
			port = E_DD_PMC_GPIO_P23;
			break;
		case 24:
			port = E_DD_PMC_GPIO_P24;
			break;
		case 25:
			port = E_DD_PMC_GPIO_P25;
			break;
		case 26:
			port = E_DD_PMC_GPIO_P26;
			break;
		case 27:
			port = E_DD_PMC_GPIO_P27;
			break;
		case 30:
			port = E_DD_PMC_GPIO_P30;
			break;
		case 31:
			port = E_DD_PMC_GPIO_P31;
			break;
		case 32:
			port = E_DD_PMC_GPIO_P32;
			break;
		case 33:
			port = E_DD_PMC_GPIO_P33;
			break;
		case 34:
			port = E_DD_PMC_GPIO_P34;
			break;
		case 35:
			port = E_DD_PMC_GPIO_P35;
			break;
		case 36:
			port = E_DD_PMC_GPIO_P36;
			break;
		case 37:
			port = E_DD_PMC_GPIO_P37;
			break;
		case 44:
			port = E_DD_PMC_GPIO_P44;
			break;
		case 45:
			port = E_DD_PMC_GPIO_P45;
			break;
		case 46:
			port = E_DD_PMC_GPIO_P46;
			break;
		default:
			break;
	}
	
	return port;
}

/*----------------------------------------------------------------------*/
/* Global Function														*/
/*----------------------------------------------------------------------*/
/**
 * @brief  Command main function for ADC test.
 * @param  int argc	:The number of parameters
 *		   char** argv	:The value of parameters
 * @return VOID
 */
VOID Ct_Dd_Pmc_Gpio_Main(int argc, char** argv)
{
	INT32				ret;
	int					param3;
	int					param4;
	E_DD_PMC_GPIO_PORT	port = E_DD_PMC_GPIO_PORT_MAX;
	UCHAR				function;
	UCHAR				direction;
	UCHAR				status;
	UCHAR				enable;
	UCHAR				control;
	
	if(strcmp(argv[1], "set") == 0){
		if(strcmp(argv[2], "epcr") == 0){
			// get 3rd paramter.
			param3 = atoi(argv[3]);
			// get 4th paramter.
			param4 = atoi(argv[4]);
			
			// port num convert.
			port = ct_dd_pmc_gpio_convert_port_num(param3);
			
			// Set EPCR
			ret = Dd_PMC_GPIO_Set_Gpio_Function(port, param4);
			if (ret == D_DDIM_OK) {
				Ddim_Print(("Dd_PMC_GPIO_Set_Gpio_Function : OK\n"));
				// register data output.
				Ddim_Print(("--- GPIO Register ---\n"));
				Ddim_Print(("EPCR10 : 0x%08x\n", IO_PMC.GPIO.EPCR.word[0]));
				Ddim_Print(("EPCR32 : 0x%08x\n", IO_PMC.GPIO.EPCR.word[1]));
				Ddim_Print(("EPCR54 : 0x%08x\n", IO_PMC.GPIO.EPCR.word[2]));
				Ddim_Print(("--------------------\n"));
			}
			else {
				Ddim_Print(("Dd_PMC_GPIO_Set_Gpio_Function : NG  ret=0x%08x\n", ret));
			}
		}
		else if(strcmp(argv[2], "ddr") == 0){
			// get 3rd paramter.
			param3 = atoi(argv[3]);
			// get 4th paramter.
			param4 = atoi(argv[4]);
			
			// port num convert.
			port = ct_dd_pmc_gpio_convert_port_num(param3);
			
			// Set DDR
			ret = Dd_PMC_GPIO_Set_Gpio_Direction(port, param4);
			if (ret == D_DDIM_OK) {
				Ddim_Print(("Dd_PMC_GPIO_Set_Gpio_Direction : OK\n"));
				// register data output.
				Ddim_Print(("--- GPIO Register ---\n"));
				Ddim_Print(("DDR10 : 0x%08x\n", IO_PMC.GPIO.DDR.word[0]));
				Ddim_Print(("DDR32 : 0x%08x\n", IO_PMC.GPIO.DDR.word[1]));
				Ddim_Print(("DDR54 : 0x%08x\n", IO_PMC.GPIO.DDR.word[2]));
				Ddim_Print(("--------------------\n"));
			}
			else {
				Ddim_Print(("Dd_PMC_GPIO_Set_Gpio_Direction : NG  ret=0x%08x\n", ret));
			}
		}
		else if(strcmp(argv[2], "pdr") == 0){
			// get 3rd paramter.
			param3 = atoi(argv[3]);
			// get 4th paramter.
			param4 = atoi(argv[4]);
			
			// port num convert.
			port = ct_dd_pmc_gpio_convert_port_num(param3);
			
			// Set PDR
			ret = Dd_PMC_GPIO_Set_Gpio_Status(port, param4);
			if (ret == D_DDIM_OK) {
				Ddim_Print(("Dd_PMC_GPIO_Set_Gpio_Status : OK\n"));
				// register data output.
				Ddim_Print(("--- GPIO Register ---\n"));
				Ddim_Print(("PDR10 : 0x%08x\n", IO_PMC.GPIO.PDR.word[0]));
				Ddim_Print(("PDR32 : 0x%08x\n", IO_PMC.GPIO.PDR.word[1]));
				Ddim_Print(("PDR54 : 0x%08x\n", IO_PMC.GPIO.PDR.word[2]));
				Ddim_Print(("--------------------\n"));
			}
			else {
				Ddim_Print(("Dd_PMC_GPIO_Set_Gpio_Status : NG  ret=0x%08x\n", ret));
			}
		}
		else if(strcmp(argv[2], "puder") == 0){
			// get 3rd paramter.
			param3 = atoi(argv[3]);
			// get 4th paramter.
			param4 = atoi(argv[4]);
			
			// port num convert.
			port = ct_dd_pmc_gpio_convert_port_num(param3);
			
			// Set PUDER
			ret = Dd_PMC_GPIO_Set_Gpio_Pull_Up_Down_Enable(port, param4);
			if (ret == D_DDIM_OK) {
				Ddim_Print(("Dd_PMC_GPIO_Set_Gpio_Pull_Up_Down_Enable : OK\n"));
				// register data output.
				Ddim_Print(("--- GPIO Register ---\n"));
				Ddim_Print(("PUDER10 : 0x%08x\n", IO_PMC.GPIO.PUDER.word[0]));
				Ddim_Print(("PUDER32 : 0x%08x\n", IO_PMC.GPIO.PUDER.word[1]));
				Ddim_Print(("PUDER54 : 0x%08x\n", IO_PMC.GPIO.PUDER.word[2]));
				Ddim_Print(("--------------------\n"));
			}
			else {
				Ddim_Print(("Dd_PMC_GPIO_Set_Gpio_Pull_Up_Down_Enable : NG  ret=0x%08x\n", ret));
			}
		}
		else if(strcmp(argv[2], "pudcr") == 0){
			// get 3rd paramter.
			param3 = atoi(argv[3]);
			// get 4th paramter.
			param4 = atoi(argv[4]);
			
			// port num convert.
			port = ct_dd_pmc_gpio_convert_port_num(param3);
			
			// Set PUDCR
			ret = Dd_PMC_GPIO_Set_Gpio_Pull_Up_Down_Ctrl(port, param4);
			if (ret == D_DDIM_OK) {
				Ddim_Print(("Dd_PMC_GPIO_Set_Gpio_Pull_Up_Down_Ctrl : OK\n"));
				// register data output.
				Ddim_Print(("--- GPIO Register ---\n"));
				Ddim_Print(("PUDCR10 : 0x%08x\n", IO_PMC.GPIO.PUDCR.word[0]));
				Ddim_Print(("PUDCR32 : 0x%08x\n", IO_PMC.GPIO.PUDCR.word[1]));
				Ddim_Print(("PUDCR54 : 0x%08x\n", IO_PMC.GPIO.PUDCR.word[2]));
				Ddim_Print(("--------------------\n"));
			}
			else {
				Ddim_Print(("Dd_PMC_GPIO_Set_Gpio_Pull_Up_Down_Ctrl : NG  ret=0x%08x\n", ret));
			}
		}
		else {
			Ddim_Print(("please check 2nd parameter!!\n"));
		}
	}
	else if(strcmp(argv[1], "get") == 0){
		if(strcmp(argv[2], "epcr") == 0){
			// get 3rd paramter.
			param3 = atoi(argv[3]);
			
			// port num convert.
			port = ct_dd_pmc_gpio_convert_port_num(param3);
			
			// data clear.
			direction = 0;
			
			// Get EPCR.
			ret = Dd_PMC_GPIO_Get_Gpio_Function(port, &function);
			if (ret == D_DDIM_OK) {
				Ddim_Print(("Dd_PMC_GPIO_Get_Gpio_Function : OK\n"));
				// register data output.
				Ddim_Print(("--- GPIO Register ---\n"));
				Ddim_Print(("EPCR10 : 0x%08x\n", IO_PMC.GPIO.EPCR.word[0]));
				Ddim_Print(("EPCR32 : 0x%08x\n", IO_PMC.GPIO.EPCR.word[1]));
				Ddim_Print(("EPCR54 : 0x%08x\n", IO_PMC.GPIO.EPCR.word[2]));
				// get data output.
				Ddim_Print(("--- Get Data -------\n"));
				Ddim_Print(("port     : %d\n", param3));
				Ddim_Print(("function : %d\n", function));
				Ddim_Print(("--------------------\n"));
			}
			else {
				Ddim_Print(("Dd_PMC_GPIO_Get_Gpio_Function : NG  ret=0x%08x\n", ret));
			}
		}
		else if(strcmp(argv[2], "ddr") == 0){
			// get 3rd paramter.
			param3 = atoi(argv[3]);
			
			// port num convert.
			port = ct_dd_pmc_gpio_convert_port_num(param3);
			
			// data clear.
			function = 0;
			
			// Get DDR.
			ret = Dd_PMC_GPIO_Get_Gpio_Direction(port, &direction);
			if (ret == D_DDIM_OK) {
				Ddim_Print(("Dd_PMC_GPIO_Get_Gpio_Direction : OK\n"));
				// register data output.
				Ddim_Print(("--- GPIO Register ---\n"));
				Ddim_Print(("DDR10 : 0x%08x\n", IO_PMC.GPIO.DDR.word[0]));
				Ddim_Print(("DDR32 : 0x%08x\n", IO_PMC.GPIO.DDR.word[1]));
				Ddim_Print(("DDR54 : 0x%08x\n", IO_PMC.GPIO.DDR.word[2]));
				// get data output.
				Ddim_Print(("--- Get Data -------\n"));
				Ddim_Print(("port      : %d\n", param3));
				Ddim_Print(("direction : %d\n", direction));
				Ddim_Print(("--------------------\n"));
			}
			else {
				Ddim_Print(("Dd_PMC_GPIO_Get_Gpio_Direction : NG  ret=0x%08x\n", ret));
			}
		}
		else if(strcmp(argv[2], "pdr") == 0){
			// get 3rd paramter.
			param3 = atoi(argv[3]);
			
			// port num convert.
			port = ct_dd_pmc_gpio_convert_port_num(param3);
			
			// data clear.
			status = 0;
			
			// Get PDR.
			ret = Dd_PMC_GPIO_Get_Gpio_Status(port, &status);
			if (ret == D_DDIM_OK) {
				Ddim_Print(("Dd_PMC_GPIO_Get_Gpio_Status : OK\n"));
				// register data output.
				Ddim_Print(("--- GPIO Register ---\n"));
				Ddim_Print(("PDR10 : 0x%08x\n", IO_PMC.GPIO.PDR.word[0]));
				Ddim_Print(("PDR32 : 0x%08x\n", IO_PMC.GPIO.PDR.word[1]));
				Ddim_Print(("PDR54 : 0x%08x\n", IO_PMC.GPIO.PDR.word[2]));
				// get data output.
				Ddim_Print(("--- Get Data -------\n"));
				Ddim_Print(("port   : %d\n", param3));
				Ddim_Print(("status : %d\n", status));
				Ddim_Print(("--------------------\n"));
			}
			else {
				Ddim_Print(("Dd_PMC_GPIO_Get_Gpio_Status : NG  ret=0x%08x\n", ret));
			}
		}
		else if(strcmp(argv[2], "puder") == 0){
			// get 3rd paramter.
			param3 = atoi(argv[3]);
			
			// port num convert.
			port = ct_dd_pmc_gpio_convert_port_num(param3);
			
			// data clear.
			enable = 0;
			
			// Get PUDER.
			ret = Dd_PMC_GPIO_Get_Gpio_Pull_Up_Down_Enable(port, &enable);
			if (ret == D_DDIM_OK) {
				Ddim_Print(("Dd_PMC_GPIO_Get_Gpio_Pull_Up_Down_Enable : OK\n"));
				// register data output.
				Ddim_Print(("--- GPIO Register ---\n"));
				Ddim_Print(("PUDER10 : 0x%08x\n", IO_PMC.GPIO.PUDER.word[0]));
				Ddim_Print(("PUDER32 : 0x%08x\n", IO_PMC.GPIO.PUDER.word[1]));
				Ddim_Print(("PUDER54 : 0x%08x\n", IO_PMC.GPIO.PUDER.word[2]));
				// get data output.
				Ddim_Print(("--- Get Data -------\n"));
				Ddim_Print(("port   : %d\n", param3));
				Ddim_Print(("enable : %d\n", enable));
				Ddim_Print(("--------------------\n"));
			}
			else {
				Ddim_Print(("Dd_PMC_GPIO_Get_Gpio_Pull_Up_Down_Enable : NG  ret=0x%08x\n", ret));
			}
		}
		else if(strcmp(argv[2], "pudcr") == 0){
			// get 3rd paramter.
			param3 = atoi(argv[3]);
			
			// port num convert.
			port = ct_dd_pmc_gpio_convert_port_num(param3);
			
			// data clear.
			control = 0;
			
			// Get PUDCR.
			ret = Dd_PMC_GPIO_Get_Gpio_Pull_Up_Down_Ctrl(port, &control);
			if (ret == D_DDIM_OK) {
				Ddim_Print(("Dd_PMC_GPIO_Get_Gpio_Pull_Up_Down_Ctrl : OK\n"));
				// register data output.
				Ddim_Print(("--- GPIO Register ---\n"));
				Ddim_Print(("PUDCR10 : 0x%08x\n", IO_PMC.GPIO.PUDCR.word[0]));
				Ddim_Print(("PUDCR32 : 0x%08x\n", IO_PMC.GPIO.PUDCR.word[1]));
				Ddim_Print(("PUDCR54 : 0x%08x\n", IO_PMC.GPIO.PUDCR.word[2]));
				// get data output.
				Ddim_Print(("--- Get Data -------\n"));
				Ddim_Print(("port    : %d\n", param3));
				Ddim_Print(("control : %d\n", control));
				Ddim_Print(("--------------------\n"));
			}
			else {
				Ddim_Print(("Dd_PMC_GPIO_Get_Gpio_Pull_Up_Down_Ctrl : NG  ret=0x%08x\n", ret));
			}
		}
		else if(strcmp(argv[2], "err") == 0){
			int num;
			num = atoi(argv[3]);
			switch( num ) {
			case 1:
				ret = Dd_PMC_GPIO_Get_Gpio_Function(E_DD_PMC_GPIO_P22, NULL);
				Ddim_Print(("Dd_PMC_GPIO_Get_Gpio_Function : NG  ret=0x%08x\n", ret));
				break;
			case 2:
				ret = Dd_PMC_GPIO_Get_Gpio_Direction(E_DD_PMC_GPIO_P20, NULL);
				Ddim_Print(("Dd_PMC_GPIO_Get_Gpio_Direction : NG  ret=0x%08x\n", ret));
				break;
			case 3:
				ret = Dd_PMC_GPIO_Get_Gpio_Status(E_DD_PMC_GPIO_P20, NULL);
				Ddim_Print(("Dd_PMC_GPIO_Get_Gpio_Status : NG  ret=0x%08x\n", ret));
				break;
			case 4:
				ret = Dd_PMC_GPIO_Get_Gpio_Pull_Up_Down_Enable(E_DD_PMC_GPIO_P20, NULL);
				Ddim_Print(("Dd_PMC_GPIO_Get_Gpio_Pull_Up_Down_Enable : NG  ret=0x%08x\n", ret));
				break;
			case 5:
				ret = Dd_PMC_GPIO_Get_Gpio_Pull_Up_Down_Ctrl(E_DD_PMC_GPIO_P20, NULL);
				Ddim_Print(("Dd_PMC_GPIO_Get_Gpio_Pull_Up_Down_Ctrl : NG  ret=0x%08x\n", ret));
				break;
			default:
				Ddim_Print(("please check 3rd parameter!!\n"));
				break;
			}
		}
		else{
			Ddim_Print(("please check 2nd parameter!!\n"));
		}
	}
	else{
		Ddim_Print(("please check 1st parameter!!\n"));
	}
}
